import { Game, canvas, ctx } from './game-state.js';

export function updateBullets() {
    Game.bullets.forEach((bullet, index) => {
        // 更新子弹位置
        bullet.x += bullet.direction.x * bullet.speed;
        bullet.y += bullet.direction.y * bullet.speed;

        // 添加边界检测，当子弹超出画布范围时移除
        const screenX = bullet.x - Game.camera.x;
        const screenY = bullet.y - Game.camera.y;
        if (screenX < -10 || screenX > canvas.width + 10 || 
            screenY < -10 || screenY > canvas.height + 10) {
            Game.bullets.splice(index, 1);
            return;
        }

        // 绘制子弹
        // 判断是否按住q键，如果是散弹状态，子弹颜色为红色
        if (Game.keys['q']) {
            ctx.fillStyle = 'red';
        } else {
            ctx.fillStyle = bullet.speed === 10 ? 'yellow' : 'green';
        }
        ctx.fillRect(screenX, screenY, 5, 5);
    });
}